<template>
<div>
  <el-table
      :data="tableData"
      border
      show-summary
      style="width: 100%">
    <el-table-column
        prop="id"
        label="购物车编号"
        width="180">
    </el-table-column>
    <el-table-column
        prop="goodsList[0].goodsName"
        label="商品名称">
    </el-table-column>
    <el-table-column
        prop="user.username"
        sortable
        label="用户名称">
    </el-table-column>
    <el-table-column
        prop="buyNum"
        sortable
        label="购买数量">
    </el-table-column>
    <el-table-column
        prop="totalPrice"
        sortable
        label="小计">
    </el-table-column>
  </el-table>
  <div>
    <hr>
    <div style="margin-left: 950px">
      <el-button @click="makeOrderAndPay" type="danger">去下单支付</el-button>
    </div>

  </div>
</div>
</template>

<script>
export default {
  name: "CartView",
  data() {
    return {
      tableData: []
    };
  },
  created() {
    this.initCartData();
  },
  methods: {
    //去下单支付
    makeOrderAndPay(){
      this.axios.post("http://localhost:8083/goods/makeOrderAndPay",this.tableData).then(resp=>{
        if(resp.data.msg=="success"){
          alert("下单成功!!")
          window.open("http://localhost:8084/pay/toPay?orderId="+resp.data.orderId +"&amount="+resp.data.amount)
        }
      })
    },
    initCartData(){
      this.axios.get("http://localhost:8083/goods/cartList").then(resp=>{
        this.tableData=resp.data
      })
    }
    ,
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' 元';
        } else {
          sums[index] = 'N/A';
        }
      });

      return sums;
    }
  }
}
</script>

<style scoped>

</style>